--[[
Copyright (C) 2006  Matt Richard

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
]]

Overlord = DongleStub("Dongle-Beta0"):New("Overlord")

local defaults = {
    profile = {}
}

function Overlord:Enable()
    self.db = self:InitializeDB("OverlordDB", defaults, "Default")
    self:SetupAddonButton()
    self:RegisterMessage("DONGLE_PROFILE_CHANGED")
    self:RegisterMessage("DONGLE_PROFILE_CREATED")
    self:ProfileChanged()
end

function Overlord:DONGLE_PROFILE_CHANGED(message,db,parent)
    if parent == self then
        self:ProfileChanged()
    end
end

function Overlord:DONGLE_PROFILE_CREATED(message,db,parent)
    if parent == self then
        self:SetupProfile()
    end
end

function Overlord:ProfileChanged()
    self:ApplyProfile()
    self:UpdateList()
end

function Overlord:SetupProfile()
    local p = self.db.profile
    for i=1,GetNumAddOns() do
        local name,_,_,enabled = GetAddOnInfo(i)
        p[name] = not enabled and 0 or nil
    end
end

function Overlord:ApplyProfile()
    local p = self.db.profile
    -- Check each addon in the DB to ensure that it's still installed
    -- If it's not present, increment the last seen variable
    for addon,sessions in pairs(p) do
        
        if not GetAddOnInfo(addon) then
            p[addon] = sessions + 1
        else
            p[addon] = 0
        end
        -- If the addon hasn't been seen in more than 15 sessions, purge it
        if p[addon] > 15 then
            p[addon] = nil
        end
    end
    for i=1,GetNumAddOns() do
        local name = GetAddOnInfo(i)
        if p[name] then
            self:DisableAddon(i)
        else
            self:EnableAddon(i)
        end
    end
end

function Overlord:EnableAddon(id)
    local name = GetAddOnInfo(id)
    if name then
        EnableAddOn(id)
        self.db.profile[name] = nil
        self:UpdateSingleButton(id)
    end
end

function Overlord:DisableAddon(id)
    local name = GetAddOnInfo(id)
    if name then
        DisableAddOn(id)
        self.db.profile[name] = 0
        self:UpdateSingleButton(id)
    end
end

function Overlord:SetupAddonButton()
    if self.addonsButton then return end
    local b = CreateFrame("Button",nil,GameMenuFrame,"GameMenuButtonTemplate")
    b:SetText("AddOns")
    b:SetPoint("TOP", GameMenuButtonMacros, "BOTTOM", 0, -1)
    GameMenuFrame:SetHeight(GameMenuFrame:GetHeight() + 25)
    GameMenuButtonLogout:SetPoint("TOP", b, "BOTTOM", 0, -1)
    b:SetScript("OnClick", function() 
        HideUIPanel(GameMenuFrame) 
        Overlord:ShowGUI()
        PlaySound("igMainMenuOption");
    end)
    self.addonsButton = b
end

function Overlord:ShowGUI()
    if not self.frame then
        self:SetupSelection()
        self:CreateGUI()
    end
    self.frame:Show()
end

function Overlord:HideGUI()
    if not self.frame then return end
    self.frame:Hide()
    self:ClearSelection(true)
    PlaySound("igMainMenuOption")
end

function Overlord:SetupSelection()
    self.selected = {}
    -- I may need to add to this later so I'll leave this as its own method
end

function Overlord:UpdateList(offset)
    if not self.frame then return end
    offset = offset or self.frame.bar:GetValue()
    for i,entry in ipairs(self.list) do
        entry:SetAddon(i + offset)    
    end
end

function Overlord:UpdateSingleButton(id, offset)
    if not self.frame then return end
    offset = offset or self.frame.bar:GetValue()
    if id <= offset + 22 and id > offset then
        self.list[id - offset]:Update(value)
    end
end

function Overlord:ClearSelection(noUpdate)
    self.frame.bar:GetValue()
    for id in pairs(self.selected) do
        self:SetSelected(id, nil, true)
        if not noUpdate then self:UpdateSingleButton(id, offset) end
    end  
end

function Overlord:SetSelected(id,value,noUpdate)
    self.selected[id] = value
    if not noUpdate then
        self:UpdateSingleButton(id)
    end
    self.selectionFocus = id
end

function Overlord:ToggleSelected(id, noUpdate)
    if self.selected[id] then
        self:SetSelected(id, nil, noUpdate)
    else
        self:SetSelected(id, true, noUpdate)
    end
end

function Overlord:GroupSelection(id)
    if not self.selectionFocus then
        self:ToggleSelected(id)
        return
    end
    local focus = self.selectionFocus
    local dir = id > focus and 1 or -1
    for i = focus + dir, id, dir do
        self:ToggleSelected(i, noUpdate)
    end
    self:UpdateList()
end

function Overlord:SelectAll()
    for i=1,GetNumAddOns() do
        self:SetSelected(i,true,true)
    end
    self:UpdateList()
end

function Overlord:SelectEnabled()
    self:ClearSelection(true)
    for i=1,GetNumAddOns() do
        local name,_,_,enabled = GetAddOnInfo(i)
        if enabled then
            self:SetSelected(i,true,true)
        end
    end
    self:UpdateList()
end

function Overlord:SelectDisabled()
    self:ClearSelection(true)
    for i=1,GetNumAddOns() do
        local name,_,_,enabled = GetAddOnInfo(i)
        if not enabled then
            self:SetSelected(i,true,true)
        end
    end
    self:UpdateList()
end

function Overlord:EnableSelected()
    for id in pairs(self.selected) do
        self:EnableAddon(id)
    end
end

function Overlord:DisableSelected()
    for id in pairs(self.selected) do
        self:DisableAddon(id)
    end
end

function Overlord:ReloadUI()
    StaticPopup_Show("OVERLORD_RELOAD_UI")
end


